package com.thinkbig.datamining.decisiontree;

import java.util.Random;

import com.thinkbig.datamining.decisiontree.PropDefine.BUY;



public class DataGenerator {

    public static void generateRawData(int num) throws InterruptedException {        
        for (int i = 0; i < num; i++) {
            Data.rawData.add(Person.getData());
            Thread.sleep((new Random()).nextInt(2));
        }
    }
    
    public static void initTree() {
        for (String[] data : Data.rawData) {
            fillTree(data);
        }
    }
    
    private static void fillTree(String[] data) {
        TreeNode tn = Data.decisionTree;
        boolean buy = String.valueOf(BUY.YES).equals(data[data.length - 1]) ? true : false;
        for (int i = 0; i < data.length - 1; i++) {
            String vec = data[i];
            TreeNode subTree = null;
            if (!tn.sons.containsKey(vec)) {
                subTree = new TreeNode(vec);
                tn.sons.put(vec, subTree);
            } else {
                subTree = tn.sons.get(vec);
            }
            subTree.buyNum = buy ? subTree.buyNum + 1 : subTree.buyNum;
            subTree.total += 1;
            tn = subTree;
        }
    }
}
